home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Mac C Primer V2
/
4.2 - Palette
/
Palette.c
next >
Wrap
C/C++ Source or Header
|
1991-08-21
|
5KB
|
258 lines
/************************************************************/
/* */
/* Palette Code from Chapter Four of */
/* */
/* *** The Macintosh Programming Primer *** */
/* */
/* Copyright 1990, Dave Mark */
/* */
/* This program demonstrates specific Mac programming */
/* techniques. */
/* */
/************************************************************/
#include "Palettes.h"
#include "Picker.h"
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define NIL_STRING "\p"
#define VISIBLE TRUE
#define HAS_GOAWAY TRUE
#define MOVE_TO_FRONT (WindowPtr)-1L
#define REMOVE_ALL_EVENTS 0
#define MIN_SLEEP 0L
#define NIL_MOUSE_REGION 0L
#define PRECISE_TOLERANCE 0x0000
#define NUM_SQUARES 150
Boolean IsColor();
WindowPtr CreateColorWindow();
PaletteHandle MakeRedPalette(), MakeBrightPalette(), MakeGrayPalette();
main()
{
Point corner;
WindowPtr window;
PaletteHandle pal;
ToolBoxInit();
if ( ! IsColor() )
DoAlert( "\pThis machine does not support Color QuickDraw!" );
else
{
corner.h = 10;
corner.v = 40;
window = CreateColorWindow( corner, "\pRed Palette" );
pal = MakeRedPalette();
SetPalette( window, pal, TRUE );
corner.h = 170;
corner.v = 177;
window = CreateColorWindow( corner, "\pBright Palette" );
pal = MakeBrightPalette();
SetPalette( window, pal, TRUE );
corner.h = 330;
corner.v = 40;
window = CreateColorWindow( corner, "\pGray Palette" );
pal = MakeGrayPalette();
SetPalette( window, pal, TRUE );
DoEventLoop();
}
}
/*********************************** ToolBoxInit */
ToolBoxInit()
{
InitGraf( &thePort );
InitFonts();
FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
InitWindows();
InitMenus();
TEInit();
InitDialogs( NIL_POINTER );
InitCursor();
}
/*********************************** DoEventLoop */
DoEventLoop()
{
Boolean done;
EventRecord e;
short part;
WindowPtr window;
done = FALSE;
while ( ! done )
{
WaitNextEvent( everyEvent, &e, MIN_SLEEP, NIL_MOUSE_REGION );
switch( e.what )
{
case mouseDown:
part = FindWindow( e.where, &window );
if ( part == inGoAway )
done = TRUE;
else if ( part == inDrag )
DragWindow( window, e.where, &screenBits.bounds );
else if ( part == inContent )
{
if ( window != FrontWindow() )
SelectWindow( window );
}
break;
case updateEvt:
BeginUpdate( (WindowPtr)e.message );
SetPort( (WindowPtr)e.message );
DrawBullseye();
EndUpdate( (WindowPtr)e.message );
break;
}
}
}
/*********************************** DrawBullseye */
DrawBullseye()
{
int i, center;
Rect r;
center = NUM_SQUARES;
for ( i=1; i<=NUM_SQUARES; i++ )
{
PmForeColor( i - 1 );
r.top = center - i;
r.left = center - i;
r.bottom = center + i;
r.right = center + i;
FrameRect( &r );
}
}
/******************************** IsColor *********/
Boolean IsColor()
{
SysEnvRec mySE;
SysEnvirons( 1, &mySE );
return( mySE.hasColorQD );
}
/******************************** MakeRedPalette *********/
PaletteHandle MakeRedPalette()
{
RGBColor c;
long i;
PaletteHandle redPalette;
redPalette = NewPalette( NUM_SQUARES, NIL_POINTER,
pmTolerant, PRECISE_TOLERANCE );
c.green = 0;
c.blue = 0;
for ( i=0; i<NUM_SQUARES; i++ )
{
c.red = (i * 65535) / NUM_SQUARES;
SetEntryColor( redPalette, i, &c );
}
return( redPalette );
}
/******************************** MakeBrightPalette *********/
PaletteHandle MakeBrightPalette()
{
PaletteHandle brightPalette;
long i;
RGBColor rgbColor;
HSVColor hsvColor;
brightPalette = NewPalette( NUM_SQUARES, NIL_POINTER,
pmTolerant, PRECISE_TOLERANCE );
hsvColor.value = 65535;
hsvColor.saturation = 65535;
for ( i=0; i<NUM_SQUARES; i++ )
{
hsvColor.hue = (i * 65535) / NUM_SQUARES;
HSV2RGB( &hsvColor, &rgbColor );
SetEntryColor( brightPalette, i, &rgbColor );
}
return( brightPalette );
}
/******************************** MakeGrayPalette *********/
PaletteHandle MakeGrayPalette()
{
PaletteHandle grayPalette;
long i;
RGBColor rgbColor;
grayPalette = NewPalette( NUM_SQUARES, NIL_POINTER,
pmTolerant, PRECISE_TOLERANCE );
for ( i=0; i<NUM_SQUARES; i++ )
{
rgbColor.red = (i * 65535) / NUM_SQUARES;
rgbColor.green = rgbColor.red;
rgbColor.blue = rgbColor.red;
SetEntryColor( grayPalette, i, &rgbColor );
}
return( grayPalette );
}
/*********************************** CreateColorWindow */
WindowPtr CreateColorWindow( corner, title )
Point corner;
Str255 title;
{
WindowPtr cWindow;
Rect r;
SetRect( &r, corner.h, corner.v, corner.h + (2 * NUM_SQUARES),
corner.v + (2 * NUM_SQUARES) );
cWindow = NewCWindow( NIL_POINTER, &r, title,
VISIBLE, noGrowDocProc, MOVE_TO_FRONT,
HAS_GOAWAY, NIL_POINTER );
return( cWindow );
}
/*********************************** DoAlert */
DoAlert( s )
Str255 s;
{
ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
NoteAlert( BASE_RES_ID, NIL_POINTER );
}